const path = require("path");
const fs = require("fs");
const commandOpt = process.env.npm_config_argv;
let site = 'default';
let port = 8000;
if (commandOpt) {
  const argv = JSON.parse(commandOpt);
  const arr = argv.original;
  let type = arr[1];
  if (type == 'serve' || type == 'build') {
    site = arr[2] && arr[2].replace("--", "") || 'default';
    port = arr[3] && arr[3].replace("--", "") || 8000;
    console.log('start:'+type+'--'+site);
  }
} 
const resolve = dir => {
  return path.join(__dirname, dir);
};

module.exports = {

  baseUrl: "/",

  chainWebpack: config => {

    config.resolve.alias
      .set("@", resolve("src"))
      .set("_c", resolve("src/components"))
      .set("_conf", resolve("config"))
      .end();
    
    config.module
      .rule("images")
      .test(/\.(gif|png|jpe?g|svg)$/i)
      .use("url-loader")
      .loader("url-loader")
      .options({
        limit: 1000,
        name: "img/[name].[hash:7].[ext]",
        publicPath: "/"
      })
      .end();
    
    config.module
      .rule("images-min")
      .test(/\.(gif|png|jpe?g|svg)$/i)
      .use("image-webpack-loader")
      .loader("image-webpack-loader")
      .options({
        mozjpeg: {
          progressive: true,
          quality: 80
        },
        optipng: {
          enabled: false
        },
        pngquant: {
          quality: "80-90",
          speed: 4
        },
        gifsicle: {
          interlaced: false
        }
      })
      .end();

    config
      .plugin("html")
      .tap(args => {
        args[0].template = "./src/site/" + site + "/index.html",
        args[0].favicon = "./src/site/" + site + "/favicon.ico"
        return args;
      })
      .end();
  },

  //lintOnSave: true, 
  
  outputDir: "./dist/" + site + "/",
  
  configureWebpack: {
    entry: "./src/site/" + site + "/main.js",
    externals: {
      "vue": "Vue",
      "vue-router": "VueRouter",
      "vuex": "Vuex",
      "axios": "axios",
      "echarts": "echarts",
      "lodash":"_",
      //vant cdn 存在BUG
    },
    plugins: []
  },

  productionSourceMap: false,

  pluginOptions: {
    
    "style-resources-loader": {
      preProcessor: "less",
      patterns: [path.resolve(__dirname, "./src/styles/global.less")]
    },
    
    "uglifyjs-webpack-plugin": {
      uglifyOptions: {
        compress: {
          warnings: false,
          drop_console: true,
          collapse_vars: true,
          reduce_vars: true
        },
        output: {
          beautify: false,
          comments: false
        }
      }
    }
  },
  
  css: {
    loaderOptions: {
      css: {
        localIdentName: "[name]-[hash]",
        camelCase: "only"
      }
    }
  },

  devServer: {

    port: port,

    //disableHostCheck: true,
    
    proxy: {
      '/api': {

        //地图
        target: 'http://hqn.fun:16812',
        pathRewrite: {'^/api': ''},
     
        ws: true,
        changeOrigin: true
      }
    }

  }
};
